Custom rewrite rule, url returning 404

I have a custom URL like this: website.com/show/?id=9999&n=page-name and I'm trying to come up with a mod_rewrite rule to convert to website.com/show/9999/page-name/ /show/ is a page name. Here's the rules I'm using in .htaccess: <IfModule mod_rewrite.c> Options -MultiViews RewriteEngine On RewriteBase / RewriteRule ^show/(.*)$ /show/?id=$1 [R=301,NC,QSA] RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> This is working, it rewrites website.com/show/?id=9999` to website.com/show/9999/ Then I modified the rule for the second query string: RewriteRule …
Category: Web

How can i redirect one url to another url using .htaccess or add_rewrite_rule

I am stuck at redirecting one url to another url. I want to redirect https://samedomain.com/page-check-927GSJAYS639AL/highlights to https://samedomain.com/page-highlights/ (927GSJAYS639AL this is id it can be anything) the http://samedomain.com/page-highlights/ is the wordpress page The rewrite rule I tied in .htaccess file (added this in the first line it-self) is: RewriteEngine On RewriteRule ^page-check-([A-Z0-9_-]+)/highlights$ page-highlights?id=$1 [L] but I see a 404 error page It worked for RewriteEngine On RewriteRule ^page-check-([A-Z0-9_-]+)/highlights$ info.php?id=$1 [L] I also tried WP rewrite API php code UPDATE 1 add_action('init', …
Category: Web

Override htacces rule only for specific directory

I have a WordPress site with ithemes security installed plugin. I want to disable this rule: RewriteCond %{HTTP_USER_AGENT} "^$" [NC,OR] only for this wp-content/uploads/xmls directory. Relevant (summarised) section of .htaccess file: # Begin HackRepair.com Blacklist RewriteEngine on # Abuse Agent Blocking RewriteCond %{HTTP_USER_AGENT} ^[Ww]eb[Bb]andit [NC,OR] RewriteCond %{HTTP_USER_AGENT} ^$ [NC,OR] RewriteCond %{HTTP_USER_AGENT} ^Acunetix [NC,OR] : : etc. : RewriteCond %{HTTP_USER_AGENT} YisouSpider [NC,OR] RewriteCond %{HTTP_USER_AGENT} zermelo [NC,OR] RewriteCond %{HTTP_USER_AGENT} ZyBorg RewriteRule ^.* - [F,L] # Abuse bot blocking rule end
Category: Web

URL Rewriting for PHP script on an image URL

I built an image randomizer so that when I open a .php file in a URL that it displays the image with readfile($randomImage);. This works locally but when I upload it to the server it gets blocked by the firewall since we do not want to be able to load URLs like https://www.example.com/wp-content/themes/THEME/randomImage.php I want to load a .php script on a .jpg file. What needs to happen is that when I use the URL (for instance) https://www.example.com/image.jpg and when …
Category: Web

add_rewrite_rule() driving me crazy, rewrite not working when analyzer says it should

I have a special situation and need some help achieving the final step. Background: When I access mydomain.com/category/postname, I get the post as expected. When I access mydomain.com/category/postname?ajax=1, I get the post html (baked into and including its corresponding page template markup, which is extremely important for this use case), but I get it rendered without its header and footer (I did this on purpose, with a $_GET['ajax']==1 conditional inside the header.php and footer.php files to suppress them). This all …
Category: Web

htaccess, site and staging in subdirectories

I have the following task and as it is a little different from most setups, I couldn't find any hints that solved my problem. WordPress for the live site was installed into a subfolder /wp1, the .htaccess directives are a rewrite to www and https followed by the usual WP directives: <IfModule mod_rewrite.c> RewriteEngine On # rewrite any request to the certified domain to use www RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # rewrite to HTTPS RewriteCond %{HTTPS} off …
Category: Web

Cache Busting using htaccess Rewrite rule?

I am trying to follow this tutorial that says i can make a server to send a file like style.css if the requested file is style.15458888.css with a rewrite rule to be put inside the htaccess file. This Rule RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.+).(d+).(js|css)$ $1.$3 [L] So i followed with this in the head tag: <?php $time = filemtime(get_template_directory() .'/assets/css/main.css');?> <link href="<?php echo get_template_directory_uri().'/assets/css/main.'.$time.'.css'?>" rel="stylesheet" type="text/css"> and this inside the Htaccess: # BEGIN WordPress <IfModule mod_expires.c> ExpiresActive On ExpiresByType image/jpg …
Category: Web

Change permalinks for posts but not for custom post types

Currently my permalink structure for posts is domain.com/post-title I'm using a static front page and a "blog" page for posts. I'd like to to change the permalink structure for posts, tags and categories to domain.com/blog/post-title I can easily add /blog/ in the permalink structure but the catch is that my custom post types also get this modified permalink structure and that is not the desired result. Thanks in advance!
Category: Web

Redirect - remove custom query string

I have a problem with some page URLs. The page URL structure is like this: www.example.com/wp/custom-taxonomy/somepagename/?token=12345 I want that when I access the URL above to redirect me to: www.example.com/wp/custom-taxonomy/somepagename/ In /wp/ is WordPress installed. I tried this in .htaccess: <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /wp/ RewriteCond %{query_string} ^token=12345 RewriteRule (.*) /$1? [R=301,L] RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /wp/index.php [L] </IfModule> but it redirects me to: www.example.com/custom-taxonomy/somepagename Any ideas? Later edit: /wp/ - …
Category: Web

Can only access homepage and admin panel

I am really struggling to move Wordpress from an external host to my localhost (xampp). After two days trying to do this I am now at the stage where I can access the homepage and admin panel, but nothing else. I have accessed permalinks and clicked save as shown below: Here is the .htaccess file that is generated: # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /Blog/ RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /Blog/index.php …
Category: Web

Change htaccess to redirect to index.php in subfolder

How can I change the WordPress root folder .htaccess file to redirect URLs with a specific subfolder to its own index.php and give the path data as a URL variable to the index.php? https://example.com/subfolder/img.png redirect to: https://example.com/subfolder/index.php?path=img.png all other redirects should work like WordPress expects them to.
Category: Web

Rewrite Rules Are Redirecting and Not Passing VARs

I have the following code in my functions.php file. <?php add_action('init', 'flush_rewrite_rules'); function custom_add_rewrite_rules( $wp_rewrite ) { $new_rules = array( 'insurance-leads-types/auto-insurance-leads/([^/\.]+)/?$' => 'index.php?pagename=auto-insurance-leads&state=' . $wp_rewrite->preg_index(1), 'insurance-leads-types/home-insurance-leads/([^/\.]+)/?$' => 'index.php?pagename=home-insurance-leads&state=' . $wp_rewrite->preg_index(1) ); $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; } add_action('generate_rewrite_rules', 'custom_add_rewrite_rules'); function add_query_vars( $query_vars ) { $query_vars[] = 'state'; return $query_vars; } add_filter( 'query_vars', 'add_query_vars' ); When I go to the page /insurance-leads-types/auto-insurance-leads/STATENAME/ the URL is redirected to /insurance-leads-types/auto-insurance-leads/ without the trailing STATENAME/ and the variable is not passed to $query_vars. I …
Category: Web

404 for custom static link on IIS with URL Rewrite Module

I need to use IIS to develop the new WordPress site. After reading a lot of articles, I installed the URL Rewrite Module on IIS (https://www.iis.net/downloads/microsoft/url-rewrite). My site structure is: localhost/projectname/projectcms/ I set the WordPress URL on cms as: http://localhost/projectname/projectcms And the wordpress was installed inside the projectcms folder. The site URL as: http://localhost/projectname Also, I customize the permalink as: http://localhost/projectname/% category%/%year%/%postname%/ (no space between "%" and "c") However, those pages display 404 errors. What is the rewrite rule I …
Category: Web

How to create a 301 redirect that doesn't apply to subsites

Scenario: I'm trying to set a 301 redirect so that https://foo.org/events is redirected to a non-WordPress site https://bar.org/events. On my .htaccess file, I set the following: Redirect 301 /events https://bar.org/events/ This worked great until I found out the rule was cascading down to my subsites, which have completely separate and unrelated event pages. In other words: https://subsite1.foo.org/events -> redirected to https://bar.org/events/ https://subsite2.foo.org/events -> redirected to https://bar.org/events/ [etc.] But I don't want this. Again, those are completely separate event pages with …
Category: Web

Menu links only using http after enabling https, unable to redirect http links

I have a WordPress website on a Bluehost shared hosting server and since I have multiple websites in my space, I have this particular folder in its own folder under public_html. This means that I have a .htaccess file in public_html, which redirects my domain to the specific website folder. I also have another .htaccess file in the website folder, which has some WordPress settings and a redirect for non-www URLs. I've tried adding this http -> https rule in …
Category: Web

Remove base slug in permalinks of hierarchical custom post type

I have a hierarchical Custom Post Type called project, registered as follows: register_post_type( 'project', array( 'public' => true, 'hierarchical' => true, 'rewrite' => array( 'with_front' => false ) ) ); The URLs currently look like this: https://example.com/project/hello https://example.com/project/hello/world I would like the URLs to look like this: https://example.com/hello https://example.com/hello/world How can I most efficiently accomplish this such that other URLs are not interfered with (e.g. pages, blog posts, other custom post types)? All the solutions that are currently out there …
Category: Web

Adding rewrite rules directly to .htaccess file

I wanna add a simple rewrite rule RewriteRule ^apply\/? https://docs.google.com/forms/d/e/1FAIpQLSf5IrOhg0E_NAGZnOvMuaXhU80sio8bukaWVBkb87eEOa9kTw/viewform [L] my .htaccess file is like # BEGIN WordPress # The directives (lines) between "BEGIN WordPress" and "END WordPress" are # dynamically generated, and should only be modified via WordPress filters. # Any changes to the directives between these markers will be overwritten. <IfModule mod_rewrite.c> RewriteEngine On RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END …
Category: Web

Rewriting subfolders to specific parent folder in Wordpress

I have created one page with name /parent/ and assigned new template template-parent.php that has some 3rd party software showing via JavaScript. I need to rewrite all subfolders to this parent so when someone tries to access domain.com/parent/something the url will remain the same, but he will be pointed to domain.com/parent/ so that loaded scripts could process the request. The code should be something like this RewriteRule ^(parent)/(.+)$ /$1/ [QSA,L] but I am not sure where to put it, should …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.