简体   繁体   中英

Redirect static url to dynamic

I'm trying to redirect existing traffic to a rewritten site that now uses dynamic URLs with RewriteRule on an Apache server, eg.

redirect whatever.com/directory/some_page to whatever.com/directory/?m=some_page

Every example of rewriting URLs shows the other way around (dynamic to static) so working examples seem hard to come by. Everything I've tried so far either does nothing or returns a 500 server error.

The original some_page is still there, but instead of an index file its now just a bunch of jpg's

It is important to understand that the examples you speak of are Rewrites, where the URL that the user sees is rewritten so that the application gets the URL that it can understand. Whereas it sounds like what you want is a Redirect which is where the URL that the customer gets changed to a new one before it gets sent to the application.

To redirect from whatever.com/directory/?m=some_page to whatever.com/directory/some_page you can do this.

RewriteBase /
RewiteCond %{QUERY_STRING} m=([^&]+) [NC]
RewriteRule ^directory/.? /directory/%1 [NC, L, R] 

To rewrite from whatever.com/directory/?m=some_page to whatever.com/directory/some_page you can do this.

RewriteBase /
RewiteCond %{QUERY_STRING} m=([^&]+) [NC]
RewriteRule ^directory/.? /directory/%1 [NC, L]

To redirect from whatever.com/directory/some_page /directory/?m=some_page you can do this.

RewriteBase /
RewriteRule ^directory/([^/]+)/? /directory/$1 [NC, L, R] 

To rewrite from whatever.com/directory/some_page /directory/?m=some_page you can do this.

RewriteBase /
RewriteRule ^directory/([^/]+)/? /directory/$1 [NC, L]

From reading your question I'm not entirely sure what you are trying to do, but one of the above should resolve it.

.htaccess was getting stuck in a loop because I couldn't get it to handle the query string (most likely because of my inexperience), so I took advantage of the 403 that was getting returned by navigating to /directory/some_page which still exists but without an index file (.htaccess denies directory listing).

The 403 now gets sent to a php error page which redirects to the correct address of /directory/?m=some_page using header() .

I know its an overhead of calling an extra page, but it doesn't happen too often (according to the server log), and it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM